package com.zlx.dao;

import com.zlx.entities.User;
import com.zlx.utils.DbUtil;
import com.zlx.utils.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserDao {
    //查所有的用户信息
    public List<User> queryUserList(Page<User> page, String uname){
        Connection conn = DbUtil.getConn();
        String sql = "select * from user where name like ? limit ?,?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<User> list = new ArrayList<>();
        User user =null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setObject(1,"%"+uname+"%");
            preparedStatement.setObject(2,page.getStart());
            preparedStatement.setObject(3,page.getPageSize());
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Integer id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Integer age = resultSet.getInt("age");
                String sex = resultSet.getString("sex");
                Double salary = resultSet.getDouble("salary");
                String birth = resultSet.getString("birth");
                String createTime = resultSet.getString("create_time");
                String password = resultSet.getString("password");
                user = new User(id,name,age,sex,salary,birth,createTime,password);

                list.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtil.close(conn,preparedStatement,resultSet);
        }
        return list;
    }
    //插入数据
    public void insertUserInfo(User user){
        String sql = "insert into user values(null,?,?,?,?,?,?,?)";
        DbUtil.dml(sql,user.getName(),user.getAge(),user.getSex(),user.getSalary(),user.getBirth(),new Date(),user.getPassword());
    }
    //删除数据
    public void deleteUserById(Integer id){
        String sql = "delete from user where id = ?";
        DbUtil.dml(sql,id);
    }
    //根据id查找用户
    public User queryUserById(Integer uid){
        Connection conn = DbUtil.getConn();
        String sql = "select * from user where id = ?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user =null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setObject(1,uid);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Integer id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Integer age = resultSet.getInt("age");
                String sex = resultSet.getString("sex");
                Double salary = resultSet.getDouble("salary");
                String birth = resultSet.getString("birth");
                String createTime = resultSet.getString("create_time");
                String password = resultSet.getString("password");
                user = new User(id,name,age,sex,salary,birth,createTime,password);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtil.close(conn,preparedStatement,resultSet);
        }
        return user;
    }
    //更新数据
    public void updateUserById(User user){
        String sql = "update user set name = ?,age=?,sex=?,salary=?,birth=? where id = ?";
        DbUtil.dml(sql,user.getName(),user.getAge(),user.getSex(),user.getSalary(),user.getBirth(),user.getId());
    }
    //查找用户是否存在
    public User checkUserExist(String uname, String upassword){
        Connection conn = DbUtil.getConn();
        String sql = "select * from user where name = ? and password = ?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user =null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setObject(1,uname);
            preparedStatement.setObject(2,upassword);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Integer id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Integer age = resultSet.getInt("age");
                String sex = resultSet.getString("sex");
                Double salary = resultSet.getDouble("salary");
                String birth = resultSet.getString("birth");
                String createTime = resultSet.getString("create_time");
                String password = resultSet.getString("password");
                user = new User(id,name,age,sex,salary,birth,createTime,password);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtil.close(conn,preparedStatement,resultSet);
        }
        return user;
    }
    //获取总记录数
    public Integer selectUserCount(String name){
        Connection conn = DbUtil.getConn();
        String sql = "select count(*) as c from user where name like ?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Integer count =null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setObject(1,"%"+name+"%");
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                count = resultSet.getInt("c");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DbUtil.close(conn,preparedStatement,resultSet);
        }
        return count;
    }
}
